//
//  ReasonAndDesAlert.m
//  Rabbit
//
//  Created by 文 on 2019/12/19.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "ImageVerifyAlert.h"

@interface ImageVerifyAlert ()

@property(strong, nonatomic)UITextField *codeTextField;

@property(strong, nonatomic)UIButton    *codeImageBtn;

@end

@implementation ImageVerifyAlert

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI {
    
    //验证码
    self.codeImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.codeImageBtn.frame = CGRectMake(0, 0, self.width, 90);
    self.codeImageBtn.tag = 20;
    [self.codeImageBtn addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.codeImageBtn];
    
    _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.width - 40, 35)];
    _codeTextField.font = font(14);
    _codeTextField.text = @"";
    _codeTextField.textColor = Light_TextColor;
    _codeTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写验证码" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    _codeTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 35)];
    _codeTextField.leftViewMode = UITextFieldViewModeAlways;
    cut(_codeTextField, 5);
     cutBorder(_codeTextField, 1, @"#2C7CF6")
    [DCSpeedy dc_setUpAcrossPartingLineWith:_codeTextField WithColor:Theme_ColorValue];
    [self addSubview:_codeTextField];
    
    CGFloat btnWidth = (self.width-50)/2;
    
    NSArray *titles = @[@"取消", @"确定"];
    
    for (int i=0; i<titles.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + 10;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitleColor:i==0?Theme_ColorValue:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:i==0?[UIColor whiteColor]:Theme_ColorValue];
        cut(button, BTN_BORDER_RADIUS);
        if (i==0) {
            cutBorder(button, 1, @"#2C7CF6")
        }
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(20+(btnWidth+10)*i, self.height-60, btnWidth, 35);
        [self addSubview:button];
    }
    
}

- (void)btnEventClick:(UIButton *)button {
    WEAKSELF
    if (button.tag == 10) {
        
        _codeTextField.text = @"";
        weakSelf.sendImgCodeBlock(NO, _codeTextField.text);
        
    } else if (button.tag == 11) {
        if ([DCSpeedy isBlankString:_codeTextField.text]) {
            [XSInfoView showInfo:@"验证码为空" onView:self.window];
            return;
        }
        if (weakSelf.sendImgCodeBlock) {
            weakSelf.sendImgCodeBlock(YES, _codeTextField.text);
        }
    } else {
        [self getImageCodeRequest:_phoneNum];
    }
}

- (void)setPhoneNum:(NSString *)phoneNum {
    
    _phoneNum = phoneNum;
    
    [self getImageCodeRequest:phoneNum];
}

- (void)getImageCodeRequest:(NSString *)phone {
    
    [HTTPManager getImageVerCodeRequest:CJStringWithFormat(@"%@/ntocc-tps-api/sms/getVerificationCodeImage?phone=%@&imageWith=200&imageHeigth=100", BasicDomainURL, phone) withParameter:nil withVC:[DCSpeedy dc_getCurrentVC] netName:@"获取图片验证码" success:^(id response, NSString *msg) {
        
            [self.codeImageBtn setImage:response forState:UIControlStateNormal];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    

}

@end
